package com.financing.common;
import com.financing.controller.agent.AgentController;
import com.financing.controller.agent.AllotController;
import com.financing.controller.agent.AllotLogController;
import com.financing.controller.agent.MeritController;
import com.financing.controller.agent.VisitController;
import com.financing.controller.app.AdminController;
import com.financing.controller.app.AppController;
import com.financing.controller.app.AppPushController;
import com.financing.controller.app.HomeController;
import com.financing.controller.finance.FinanceLogController;
import com.financing.controller.finance.TransferController;
import com.financing.controller.invest.BidCheckController;
import com.financing.controller.invest.BidController;
import com.financing.controller.invest.BidVirtualLogController;
import com.financing.controller.invest.CouponController;
import com.financing.controller.invest.CouponLogController;
import com.financing.controller.invest.InvestLogController;
import com.financing.controller.invest.RepayController;
import com.financing.controller.invest.ShareBenefitController;
import com.financing.controller.invest.ShareBenefitTempletController;
import com.financing.controller.invest.TradeController;
import com.financing.controller.report.ReportController;
import com.financing.controller.report.SqlReportController;
import com.financing.controller.system.LoggerController;
import com.financing.controller.system.NoticeController;
import com.financing.controller.system.ParamController;
import com.financing.controller.system.SliderController;
import com.financing.controller.system.SmsLogController;
import com.financing.controller.system.SystemController;
import com.financing.controller.system.TaskController;
import com.financing.controller.user.BankCardController;
import com.financing.controller.user.BankController;
import com.financing.controller.user.DepositController;
import com.financing.controller.user.FundController;
import com.financing.controller.user.StaffController;
import com.financing.controller.user.UserController;
import com.financing.controller.user.UserRefereeController;
import com.financing.controller.user.WithdrawController;
import com.financing.inceptor.ShiroTag;
import com.financing.inceptor.TokenInceptor;
import com.financing.model._MappingKit;
import com.financing.service.user.TokenService;
import com.financing.task.invest.AutoRepayTask;
import com.financing.task.invest.AutoSubmitBid;
import com.financing.task.invest.CouponExpireTask;
import com.financing.task.user.BirthdayNotifyTask;
import com.financing.task.user.ChangeBankCardTask;
import com.financing.util.DateUtil;
import com.financing.util.Str;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

/**
 * API引导式配置
 */
public class CoreConfig extends JFinalConfig {
    /**
     * 供Shiro插件使用。
     */
    Routes routes; 
     
	/** 
	 * 配置常量   
	 */ 
	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("app-config.txt"); 
		me.setBaseUploadPath("attachment/temp/");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setMaxPostSize(20971520);
	}  
	
	/** 
	 * 配置路由 
	 */
	@Override
	public void configRoute(Routes me) {
		this.routes = me;
		me.add("/", HomeController.class,"/view/home"); 
		me.add("/app", AppController.class,"/view/app"); 
		me.add("/admin", AdminController.class,"/view/admin"); 
		me.add("/task", TaskController.class);
		me.add("/allot", AllotController.class);
		me.add("/user", UserController.class); 
		me.add("/fund", FundController.class); 
		me.add("/bank", BankController.class);
		me.add("/bankCard", BankCardController.class);
		me.add("/appPush", AppPushController.class); 
		me.add("/allot_log", AllotLogController.class); 
		me.add("/bid", BidController.class); 
		me.add("/bidCheck", BidCheckController.class); 
		me.add("/trade", TradeController.class); 
		me.add("/sys", SystemController.class); 
		me.add("/slider", SliderController.class); 
		me.add("/notice", NoticeController.class); 
		me.add("/deposit", DepositController.class); 
		me.add("/withdraw", WithdrawController.class); 
		me.add("/investLog", InvestLogController.class);  
		me.add("/coupon", CouponController.class); 
		me.add("/couponLog", CouponLogController.class); 
		me.add("/repay", RepayController.class);  
		me.add("/logger", LoggerController.class);
		me.add("/visit", VisitController.class);
		me.add("/staff", StaffController.class);
		me.add("/merit", MeritController.class);
		me.add("/agent", AgentController.class);
		me.add("/report", ReportController.class);
		me.add("/param", ParamController.class);
		me.add("/smsLog", SmsLogController.class);
		me.add("/transfer", TransferController.class);
		me.add("/sqlReport", SqlReportController.class);
		me.add("/financeLog", FinanceLogController.class); 
		me.add("/userReferee", UserRefereeController.class); 
		me.add("/shareBenefit", ShareBenefitController.class);
		me.add("/bidVirtualLog", BidVirtualLogController.class);
		me.add("/shareBenefitTemplet", ShareBenefitTempletController.class);
	}  
	  
	@Override
	public void configEngine(Engine me) {
		me.addSharedObject("path", "");
		me.addSharedObject("assets", "/assets");
		me.addSharedFunction("/view/app/common/layout.html");
		me.addSharedFunction("/view/home/common/layout.html");
		me.addSharedFunction("/view/admin/common/layout.html"); 
		me.addSharedObject("date", new DateUtil());
		me.addSharedObject("str", new Str());
        me.addSharedObject("shiro", new ShiroTag());
        me.addSharedObject("token", new TokenService());
        me.addSharedObject("CONST", new CONST());
	} 
	
	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}
	
	/**  
	 * 配置插件 
	 */
	@Override
	public void configPlugin(Plugins me) {
		
		// 配置C3p0数据库连接池插件
		DruidPlugin druidPlugin = createDruidPlugin();
		//druidPlugin.setValidationQuery("select count(*) from DUAL");
		//druidPlugin.setTestWhileIdle(true);
		//druidPlugin.setTestOnBorrow(true);
		//druidPlugin.setTestOnReturn(true);
		//druidPlugin.setTimeBetweenConnectErrorMillis(10000);
		me.add(druidPlugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp); 

		Cron4jPlugin task = new Cron4jPlugin(); 
		task.addTask("*/10 * * * *", new AutoSubmitBid()); 
		task.addTask("0 * * * *", new CouponExpireTask());  
		task.addTask("0 * * * *", new ChangeBankCardTask()); 
		task.addTask("0 16 * * *", new AutoRepayTask());  
		task.addTask("0 12 * * *", new BirthdayNotifyTask()); 
		me.add(task); 
		
		// 加载Shiro插件
		// me.add(new ShiroPlugin(routes));
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
		shiroPlugin.setLoginUrl("/sys/toLoign");
		shiroPlugin.setSuccessUrl("/index.do");
		shiroPlugin.setUnauthorizedUrl("/sys/unAuth");
		me.add(shiroPlugin);
	}
	
	/**
	 * 配置全局拦截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		me.addGlobalActionInterceptor(new TokenInceptor());
		me.add(new SessionInViewInterceptor());  
		me.add(new TxByMethodRegex("(.*save.*|.*update.*)"));
		me.add(new ShiroInterceptor());
	} 
	 
	/**
	 * 配置处理器
	 */
	@Override
	public void configHandler(Handlers me) {
		
	}
}
